package com.hspedu.hspliving.commodity.service.impl;

import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hspedu.common.utils.PageUtils;
import com.hspedu.common.utils.Query;

import com.hspedu.hspliving.commodity.dao.CategoryDao;
import com.hspedu.hspliving.commodity.entity.CategoryEntity;
import com.hspedu.hspliving.commodity.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listTree() {
        List<CategoryEntity> entities = baseMapper.selectList(null);

        //使用stream+递归
        //使用filter返回1级分类
        List<CategoryEntity> categoryEntityList = entities.stream().filter(categoryEntity -> {
            return categoryEntity.getParentId() == 0;
        }).map(categoryEntity -> {
            //使用map,递归设置子分类
            categoryEntity.setChildrenCategories(getChildrnCategories(categoryEntity, entities));
            return categoryEntity;
        }).sorted((c1, c2) -> {
            return (c1.getSort() != null ? c1.getSort() : 0) -
                    (c2.getSort() != null ? c2.getSort() : 0);
        }).collect(Collectors.toList());

        //collect收集到集合
        return categoryEntityList;
    }

    private List<CategoryEntity> getChildrnCategories(CategoryEntity root, List<CategoryEntity> entities) {
        //设置 root的子分类
        List<CategoryEntity> categoryEntityList = entities.stream().filter(categoryEntity -> {
            return Objects.equals(categoryEntity.getParentId(), root.getId());
        }).map(categoryEntity -> {
            //使用map,递归设置子分类
            categoryEntity.setChildrenCategories(getChildrnCategories(categoryEntity, entities));
            return categoryEntity;
        }).sorted((c1, c2) -> {
            return (c1.getSort() != null ? c1.getSort() : 0) -
                    (c2.getSort() != null ? c2.getSort() : 0);
        }).collect(Collectors.toList());

        return categoryEntityList;
    }

    @Override
    public Long[] getCascadedCategoryId(Long categoryId) {
        List<Long> cascadedCategoryId = new ArrayList<>();
        getParentCategoryId(categoryId, cascadedCategoryId);
        //将顺序翻转
        Collections.reverse(cascadedCategoryId);
        return cascadedCategoryId.toArray(new Long[cascadedCategoryId.size()]);
    }

    /**
     * 该方法递归的找到 categoryId 的所有父分类 id , 并存放到 categories
     * 注意得到的所有分类 id 是一个逆序的
     * @param categoryId
     * @param categories
     * @return
     */
    private List<Long> getParentCategoryId(Long categoryId,List<Long> categories){
        //1、将当前分类 id 放入 categories 数组
        categories.add(categoryId);
        CategoryEntity categoryEntity = this.getById(categoryId);
        //是long,判断必须类型相同
        if(categoryEntity.getParentId() !=0L){
            getParentCategoryId(categoryEntity.getParentId(),categories);
        }
        return categories;
    }
}
